<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Bill\Controllers;


use App\Eloquent\Ygt\BillCategroy;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class CategoryController extends Controller
{
    public $userId          = 25;
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $type               = request('type',1);
        if( !$userInfo )
        {
            xThrow( ERR_PARAMETER );
        }
        $type               = $type == 1 ? $type : 2;
        $companyId          = $userInfo->company_id;
        $lists              = BillCategroy::getAllList($companyId,$type);
        return $lists;
    }
    public function son()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $pid                = request('pid',0);
        if( !$userInfo )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['company_id'=>$companyId,'pid'=>$pid];
        $fields             = 'id,company_id,pid,title';
        $lists              = BillCategroy::getList($where,$fields)->toArray();
        return $lists;
    }
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        if( !$userInfo )
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId              = $userInfo->company_id;
        $data                   = request()->all();
        $data['company_id']   = $companyId;
        $fieldList              = BillCategroy::$tableFieldList;
        $tableData              = BillCategroy::setDataByTableField($data,$fieldList);
        $tableDataCheckResult   = BillCategroy::checkDataByTableField($tableData,$fieldList);
        if($tableDataCheckResult !== true)
        {
            xThrow(ERR_PARAMETER, $tableDataCheckResult);
        }
        $editResult             = BillCategroy::editData($tableData);
        if($editResult===false)
        {
            xThrow(ERR_UNKNOWN);
        }
        $result         = ['message'=>'编辑成功'];
        return $result;
    }
    public function detail()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        if(!$userInfo || !$id)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId              = $userInfo->company_id;
        $where                  = ['company_id'=>$companyId,'id'=>$id];
        $info                   = BillCategroy::getInfo($where);
        if(!$info)
        {
            xThrow(ERR_UNKNOWN);
        }
        $result         = $info->toArray();
        return $result;
    }
    public function del()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $id                 = request('id',0);
        if(!$userInfo || !$id)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId              = $userInfo->company_id;
        $where                  = ['company_id'=>$companyId,'id'=>$id];
        $info                   = BillCategroy::getInfo($where);
        if(!$info)
        {
            xThrow(ERR_UNKNOWN,'该分类不存在');
        }
        $delResult              = BillCategroy::del($where);
        if(!$delResult)
        {
            xThrow(ERR_UNKNOWN);
        }
        //如果是一级分类则子分类也删除
        $pid                    = $info->pid;
        if($pid==0)
        {
            $where              = ['company_id'=>$companyId,'pid'=>$id];
            BillCategroy::del($where);
        }
        return ['message'=>'删除成功'];
    }
}